package org.dtrd.modules.suggest.service.impl;

import org.dtrd.modules.suggest.entity.po.DtrdEntRdDoctorSigninSuggestCategory;
import org.dtrd.modules.suggest.mapper.DtrdEntRdDoctorSigninSuggestCategoryMapper;
import org.dtrd.modules.suggest.service.IDtrdEntRdDoctorSigninSuggestCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.dtrd.modules.suggest.entity.bean.DoctorSigninSuggestCategoryDetail;
import org.dtrd.modules.suggest.entity.bean.DoctorSigninSuggestCategoryListInfo;
import org.dtrd.modules.suggest.entity.bean.DoctorSigninSuggestCategoryListRequest;
import org.dtrd.modules.suggest.entity.bean.DoctorSigninSuggestCategoryPageRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 医生端打卡建议分类表 服务实现类
 * </p>
 *
 * @author sun
 * @since 2022-09-21
 */
@DS("multi-datasource1")
@Service
public class DtrdEntRdDoctorSigninSuggestCategoryServiceImpl extends ServiceImpl<DtrdEntRdDoctorSigninSuggestCategoryMapper, DtrdEntRdDoctorSigninSuggestCategory> implements IDtrdEntRdDoctorSigninSuggestCategoryService {

    private DtrdEntRdDoctorSigninSuggestCategoryMapper dtrdEntRdDoctorSigninSuggestCategoryMapper;

    @Override
    public List<DoctorSigninSuggestCategoryListInfo> getDoctorSigninSuggestCategoryList(DoctorSigninSuggestCategoryListRequest request) {
        LambdaQueryWrapper<DtrdEntRdDoctorSigninSuggestCategory> queryWrapper = baseQueryWrapper();
        queryWrapper.eq(DtrdEntRdDoctorSigninSuggestCategory::getOrgCode, request.getOrgCode());
        List<DtrdEntRdDoctorSigninSuggestCategory> poList = list(queryWrapper);
        List<DoctorSigninSuggestCategoryListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(DoctorSigninSuggestCategoryListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public IPage<DoctorSigninSuggestCategoryListInfo> pageDoctorSigninSuggestCategory(DoctorSigninSuggestCategoryPageRequest request) {
        LambdaQueryWrapper<DtrdEntRdDoctorSigninSuggestCategory> queryWrapper = baseQueryWrapper();
        IPage<DtrdEntRdDoctorSigninSuggestCategory> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.page(page, queryWrapper);
        return page.convert(DoctorSigninSuggestCategoryListInfo::new);
    }

    @Override
    public DoctorSigninSuggestCategoryDetail getDoctorSigninSuggestCategory(Integer dataId) {
        DtrdEntRdDoctorSigninSuggestCategory dtrdEntRdDoctorSigninSuggestCategory = getById(dataId);
        return Optional.ofNullable(dtrdEntRdDoctorSigninSuggestCategory)
                        .map(po -> new DoctorSigninSuggestCategoryDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateDoctorSigninSuggestCategory(DoctorSigninSuggestCategoryDetail dto, String username) {
        DtrdEntRdDoctorSigninSuggestCategory dtrdEntRdDoctorSigninSuggestCategory = new DtrdEntRdDoctorSigninSuggestCategory();
        if(dto.getDataId() == null){
            dtrdEntRdDoctorSigninSuggestCategory = dtrdEntRdDoctorSigninSuggestCategory.init();
        }
        dtrdEntRdDoctorSigninSuggestCategory.parseFromDto(dto);
        dtrdEntRdDoctorSigninSuggestCategory.setCreateBy(username);
        return saveOrUpdate(dtrdEntRdDoctorSigninSuggestCategory);
    }

    @Override
    public boolean removeDoctorSigninSuggestCategory(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdEntRdDoctorSigninSuggestCategory> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntRdDoctorSigninSuggestCategory.class)
                        .eq(DtrdEntRdDoctorSigninSuggestCategory::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntRdDoctorSigninSuggestCategory> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntRdDoctorSigninSuggestCategory.class)
                        .eq(DtrdEntRdDoctorSigninSuggestCategory::getIsDel, 0);
    }
}
